<template>
  <div class="member-order">
    <!-- tab栏组件 -->
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPanel v-for="item in orderStatus" :key="item.name" :label="item.label" :name="item.name" />
    </XtxTabs>
    <!-- 订单列表 -->
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem @on-logistics="handlerLogistics" @on-confirm="handlerConfirm" @on-delete="handlerDelete"
        @on-cancel="handlerCancel" v-for="item in orderList" :key="item.id" :order="item"></OrderItem>
    </div>
    <!-- 分页组件 -->
    <XtxPageination v-if="total>0" :currentPage="reqParams.page" :pageSize="reqParams.pageSize" :total="total"
      @current-page="reqParams.page=$event" />
    <!-- 取消原因组件 -->
    <OrderCancel ref="orderCancelCom" />
    <!-- 查看物流组件 -->
    <OrderLogistics ref="orderLogisticsCom" />
  </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/api/constants'
import OrderItem from './components/order-item.vue'
import { findOrderList, delteOrder, confirmOrder } from '@/api/order'
import OrderCancel from './components/order-cancel.vue'
import Confirm from '@/components/lilbrary/Confirm'
import Message from '@/components/lilbrary/Message'
import OrderLogistics from '@/views/member/order/components/order-logistics.vue'

export default {
  name: 'MemberOrder',
  components: { OrderItem, OrderCancel, OrderLogistics },
  setup() {
    const activeName = ref('all')
    const loading = ref(false)
    const total = ref(0)

    // 获取数据
    const reqParams = reactive({
      page: 1,
      pageSize: 4,
      orderState: 0
    })
    const orderList = ref([])
    // 请求获取订单列表
    const getOrderList = () => {
      loading.value = true
      findOrderList(reqParams).then(data => {
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }

    watch(reqParams, () => {
      getOrderList()
    }, { immediate: true })

    const tabClick = ({ index }) => {
      reqParams.page = 1
      reqParams.orderState = index
    }

    // 删除订单
    const handlerDelete = (item) => {
      Confirm({ text: '您确认删除该条订单吗？' }).then(() => {
        delteOrder([item.id]).then(() => {
          Message({ text: '删除订单成功', type: 'success' })
          getOrderList()
        })
      }).catch(e => { })
    }

    return { activeName, tabClick, orderStatus, orderList, loading, total, reqParams, ...useCancel(), handlerDelete, ...useConfirm(), ...useLogistics() }
  }
}

// 取消订单
export const useCancel = () => {
  const orderCancelCom = ref(null)
  const handlerCancel = (order) => {
    orderCancelCom.value.open(order)
  }
  return { handlerCancel, orderCancelCom }
}

// 确认收货
export const useConfirm = () => {
  const handlerConfirm = (order) => {
    Confirm({ text: '您确认收到货吗？确认后货款将会打给卖家。' }).then(() => {
      confirmOrder(order.id).then(() => {
        Message({ text: '确认收货成功', type: 'success' })
        // 确认收货后状态变成 待评价
        order.orderState = 4
      })
    }).catch((e) => { })
  }
  return { handlerConfirm }
}

// 查看物流
export const useLogistics = () => {
  const orderLogisticsCom = ref(null)
  const handlerLogistics = (order) => {
    orderLogisticsCom.value.open(order)
  }
  return { handlerLogistics, orderLogisticsCom }
}
</script>

<style scoped lang="less">
.member-order {
  height: 100%;
  background: #fff;
}

.order-list {
  background: #fff;
  padding: 20px;
  min-height: 400px;
  position: relative;
}

.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, .9) url(../../../assets/images/loading.gif) no-repeat center;
}

.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
